﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO：1、所有设计像素、距离的控件，都要考虑系统缩放问题
    ///       2、所有控件的变量类型规范
    ///       3、所有控件的的输入的可以是变量
    ///       4、变量读取需要使用eval来读取变量的各种属性
    ///       5、元素库图片库系统
    ///       6、错误处理机制的完善
    ///       7、完善校验机制
    /// </summary>
    public abstract class BaseControlActions {
        //更新信息委托
        public delegate void UpdateInfoDelegate(int stepNum);
        public static UpdateInfoDelegate? updateInfoDelegate;
        public abstract int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems);
        public int ExecuteAction(FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) { 
            Type actionType = Type.GetType(BaseNameSpace.FLOW_CONTROL_BASE_PATH + item.ControlKey + "Action," + DefaultContents.PROJECT_NAME);
            BaseControlActions action = (BaseControlActions)Activator.CreateInstance(actionType);
            int endIndex = item.Index;
            int exceptionHandleType = item.ControlParam.ExceptionHandler;
            try { 
                endIndex = action.Execute(item.Index, item, globleData, flowItems);
            }catch (Exception ex) { 
                //此处应该添加日志记录，添加日志框架和本地云端日志收集框架
                if(0 == exceptionHandleType) {
                    throw ex;
                } else if(1 == exceptionHandleType) {
                    //如果是流程块途中出错，想要跳过此步骤需要直接跳到流程块尾部
                    if(item.EndTag != null) {
                        endIndex = item.EndTag.Index;
                    }
                } else {
                    throw ex;
                    //重试任务暂且搁置，之后做了重试次数的限制后再做
                }
            }
            return endIndex;
        }
    }
}
